public class demo01 {
//    顺序打印-进阶版:
//    有三个线程，分别只能打印A，B和C
//    要求按顺序打印ABC，打印10次
//    输出示例：
//    ABC
//    ABC
//    ABC
//    ABC
//    ABC
//    ...
    volatile static Object lock = new Object();
    public static int count = 0;
    public static void main(String[] args) {
        Thread t1=new Thread(()->{
            while (true) {
                synchronized (lock) {
                    while (count % 3 == 0) {
                        System.out.print("A");
                        count++;
                        lock.notify();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        Thread t2=new Thread(()->{
            while (true) {
                synchronized (lock) {
                    while (count % 3 == 1) {
                        System.out.print("B");
                        count++;
                        lock.notify();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        Thread t3=new Thread(()->{
            while (true) {
                synchronized (lock) {
                    while (count % 3 == 2) {
                        System.out.println("C");
                        count++;
                        lock.notify();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}
